package com.neuedu.mvc.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * js -->    http://localhost:8080/web05/ajax
 */
@WebServlet("/ajax")
public class AjaxController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //根据请求参数，查询推荐


        String userJson = "{'userid':1,'username':'admin'}";
        System.out.println("userJson = " + userJson);

        //发送json字符串，到浏览器

        //发送的数据类型
        resp.setContentType("application/json;charset=utf-8");

        //发送数据
        PrintWriter out = resp.getWriter();
        out.write(userJson);
        out.flush();
        out.close();
    }
}

